# -*- coding: utf-8 -*-
"""
Created on Wed Dec  4 14:34:35 2013
This module is used for discovering information from the data of apu
@author: xiehanting
"""

"""
Import packages
"""
import csv;


"""
Define parameters
"""
class parameters:
    THISYEAR = 2012;    #depend on how you calculate the age
    AGE_LOW = 5;   #default lower bound age we are interested in
    AGE_HIGH = 120; #default higher bound age we are interested in
    STEP = 5;



"""
Function --listByage
Find the user id list by the interested ages.
age_low : bigger than or equals to this age.
age_high : smaller than this age(not equals to).
"""
def listByage(age_low, age_high, reader):
    result = [];
    for row in reader:
        if row[3]!="":
            age = parameters.THISYEAR-int(row[3]);
            if (age>=age_low and age<age_high ):
                result.append(row[2]);
    return result;
    


"""    
Load files
"""
apuReader = csv.reader(open('/Users/xiehanting/Documents/SkyDrive/Documents\
/Important/PHD Research/WE R/Kontagent Data/January Kontagent Data/apu 201201.csv','rb'));
next(apuReader);    #skip the first line
listOFpeople = list(apuReader);
nOFpeople = len(listOFpeople);  #number of people in the dataset
male_c=0; #number of males in dataset
female_c=0; #number of females in data set


"""
Discover the age distribution of players
"""
for age_st in range(parameters.AGE_LOW,parameters.AGE_HIGH, parameters.STEP):
    if (age_st+1==parameters.AGE_HIGH):
        break;
    else:
        age_en = age_st + parameters.STEP +1;
        if age_st!=parameters.AGE_LOW:
            age_st=age_st+1;
        age_community = [];#name list of interesting age
        age_community = listByage(age_st, age_en, listOFpeople);
        nOFage = len(age_community);#number of instances in the list
        percentage_age = round(float(nOFage)/float(nOFpeople),4) * 100
        print("The perception of people between age " +str(age_st)+" and age "+str(age_en-1)\
            +" is "+str(percentage_age)+"%");

"""
Discover the gender distribution of players
"""
for genders in listOFpeople:
    if (genders[4]!=""):
        if(genders[4]=="m"):
            male_c+=1;
        else:
            female_c+=1;

percentage_male = round(float(male_c)/float(nOFpeople),4) * 100;
percentage_female = round(float(female_c)/float(nOFpeople),4) * 100;

print("The perception of male players is "+str(percentage_male)+"%");
print("The perception of female players is "+str(percentage_female)+"%");








